/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * PrivateChatPair.java
 * identity
 * 2014年3月4日下午5:43:34
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.entity.nosql;

import java.util.HashSet;
import java.util.Set;

import org.joda.time.DateTime;
import org.mongodb.morphia.annotations.Embedded;

@Embedded
public class PrivateChatPair {
	private UserInfo sender;
	private UserInfo receiver;
	private long reqDateTime;

	public PrivateChatPair() {
		super();
		this.reqDateTime = DateTime.now().getMillis();
	}

	public PrivateChatPair(UserInfo sender, UserInfo receiver) {
		this();
		this.sender = sender;
		this.receiver = receiver;
	}

	public UserInfo getSender() {
		return sender;
	}

	public UserInfo getReceiver() {
		return receiver;
	}

	public long getReqDateTime() {
		return reqDateTime;
	}

	public void setSender(UserInfo sender) {
		this.sender = sender;
	}

	public void setReceiver(UserInfo receiver) {
		this.receiver = receiver;
	}

	public void setReqDateTime(long reqDateTime) {
		this.reqDateTime = reqDateTime;
	}

	@Override
	public String toString() {
		return String.format("%s->%s", sender.getName(), receiver.getName());
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof PrivateChatPair))
			return false;
		PrivateChatPair target = PrivateChatPair.class.cast(obj);
		if (target.getSender() == null || target.getReceiver() == null
				|| this.getSender() == null || this.getReceiver() == null)
			return false;

		return (this.sender.getName().equalsIgnoreCase(target.sender.getName())
				&& this.receiver.getName().equalsIgnoreCase(
						target.receiver.getName()))
						
			    ||
			    (
			    		this.sender.getName().equalsIgnoreCase(target.receiver.getName())
						&& this.receiver.getName().equalsIgnoreCase(
								target.sender.getName())
			    )
						;
	}

	@Override
	public int hashCode() {
		Set<String> hset = new HashSet<String>(2);
		hset.add(this.sender.getName());
		hset.add(this.receiver.getName());
		StringBuilder sb = new StringBuilder();
		for(String s:hset){
			sb.append(s);
		}
		return sb.toString().hashCode();
		
	}
	
	
}
